package com.yay.rental.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yay.rental.R;
import com.yay.rental.provider.RemoteRentalProvider;
import com.yay.rental.provider.RentalContract.Rental;
import com.yay.rental.util.UIUtils;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

/**
 * Get Started!
 * @author team@getstarted.com.ua
 * 
 * {@link ListActivity} that displays a set of {@link Rentals}, as requested
 * through {@link Intent#getData()}.
 */
public class RentalActivity extends ListActivity {
    private RentalAdapter mAdapter;
    private RemoteRentalProvider mProvider;
    private List<model.Rental> rentals;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rental);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("query", "normandie");
        params.put("client_id", "123456");
        mProvider = new RemoteRentalProvider();
        rentals = mProvider.list(params);
        
        mAdapter = new RentalAdapter(this);
        setListAdapter(mAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAdapter = null;
        mProvider = null;
        rentals = null;
    }
    
    /** Handle "home" title-bar action. */
    public void onHomeClick(View v) {
        UIUtils.goHome(this);
    }

    /** Handle "search" title-bar action. */
    public void onSearchClick(View v) {
        /*Map<String, Object> params = new HashMap<String, Object>();
        params.put("query", "normandie");
        params.put("client_id", "123456");
        List<model.Rental> rentals = new RemoteRentalProvider().list(params);
        int debug = 0;*/
    }
    
    
    class RentalAdapter extends BaseAdapter {
        private Context mContext;
        
        public RentalAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return rentals.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
        	final model.Rental rental = rentals.get(position);
        	View view = getLayoutInflater().inflate(R.layout.list_item_rental, parent, false);
        	final TextView nameView = (TextView) view.findViewById(R.id.rental_name);
        	final TextView accView = (TextView) view.findViewById(R.id.rental_accommodations);
        	final RatingBar rating = (RatingBar) view.findViewById(R.id.rating);
            final TextView priceView = (TextView) view.findViewById(R.id.rental_price);
            final View icon = (View) view.findViewById(R.id.icon1);

            nameView.setText(rental.getName());
            
            StringBuilder acc = new StringBuilder(16);
            acc.append(rental.getBedrooms()).append(" ").append(mContext.getResources().getString(R.string.rental_br)).append(", ").
            append(rental.getBathroom()).append(" ").append(mContext.getResources().getString(R.string.rental_ba)).append(" ");
            accView.setText(acc.toString());
            
            rating.setRating(rental.getRating());
            
            StringBuilder price = new StringBuilder(16);
            price.append(rental.getPrice_min()).append("-").
            append(rental.getPrice_max()).append(" ").
            append(rental.getCurency()).append("/").
            append(rental.getUnit());
            priceView.setText(price.toString());
            
            UIUtils.fetchDrawableOnThread(rental.getThumb(), icon);
            icon.setVisibility(View.VISIBLE);

            view.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					final Uri rentalUri = Rental.buildRentalUri(rentals.get(position).getId());
			        final Intent intent = new Intent(Intent.ACTION_VIEW, rentalUri);
			        startActivity(intent);
				}
            });
            
            return view;
        }
    }
    
}
